package com.offcn.test;

import java.io.Serializable;

/**
 * @Author: zola
 * @Date: 2021/9/25 - 09 - 25 - 14:55
 * @Description: 单例模式:始终保证创建的对象只有一个
 * @Version: 1.0
 */
public class People implements Serializable {
    private static final long serialVersionUID = 1560602493466872055L;

    private Integer pid;
    private String name;
    // 3.定义对象:直接创建
    private static People student = new People(1001,"zhangsan"); // 单例模式中的 饿汉式 模式

    // 1.构造方法私有化
    private People(Integer pid, String name) {
        System.out.println("创建了People对象");
        this.pid = pid;
        this.name = name;
    }

    // 2.对外提供一个暴露的接口
    public static People getInstance(Integer sid, String name){
        if (student == null){
            student = new People(sid, name); // 如果student为null,则穿建一个并赋值给定义号的student
        }
        return student; // 不为null,则返回该定义的对象
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static People getStudent() {
        return student;
    }

    public static void setStudent(People student) {
        People.student = student;
    }
}
